package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Models.InventoryUsage;
import Models.FGI;
import Models.SpclPrice;

public class FGIPresenter extends AbstractPresenter<FGI> {
	final String SINGLEITEM = "finishedGoodsInventory";
	final String MULTIITEM = "finishedGoodsInventories";
	
	public ArrayList<FGI> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public FGI tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public FGI createItemFromJSONObject(JSONObject jsonObject){
		FGI item = new FGI();
		try{
			item.setFgiID(jsonObject.getInt("fgiid"));
			item.setFgiDescription(jsonObject.getString("FGIDescription"));
			item.setFgiPrice(jsonObject.getDouble("FGIPriceCode"));
			item.setFgiQtyAllocated(jsonObject.getInt("FGIQtyAllocated"));
			item.setFgiQtyInProduction(jsonObject.getInt("FGIQtyInProduction"));
			item.setFgiQtyOnHand(jsonObject.getLong("FGIQtyOnHand"));
			item.setServingVesselVolume(jsonObject.getDouble("servingTankVolume"));
			item.setVersion(jsonObject.getLong("version"));
			
			BeerViewPresenter beerPresenter = new BeerViewPresenter();
			JSONObject jsonBeer = jsonObject.getJSONObject("FGIBeerID");
			item.setBeer(beerPresenter.createItemFromJSONObject(jsonBeer));
			
			PackageTypePresenter ptPresenter = new PackageTypePresenter();
			JSONObject jsonPT = jsonObject.getJSONObject("FGIPackageID");
			item.setPackageType(ptPresenter.createItemFromJSONObject(jsonPT));
			
			//Set Inventory Usage Collection
			ArrayList<InventoryUsage> invUsages = new ArrayList<InventoryUsage>();
			if(!jsonObject.isNull("invUsageCollection")){
				Object itm = jsonObject.get("invUsageCollection");
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						invUsages.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					invUsages.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setInventoryUsages(invUsages);
			}
			//Set Special Price Collection
			ArrayList<SpclPrice> spclPrices = new ArrayList<SpclPrice>();
			if(!jsonObject.isNull("spclPriceCollection")){
				Object itm = jsonObject.get("spclPriceCollection");
				SpclPricePresenter presenter = new SpclPricePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						spclPrices.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					spclPrices.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setSpecialPrices(spclPrices);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(FGI item){
		try{
			JSONObject json = new JSONObject();
			json.put("fgiid", item.getFgiID());
			json.put("FGIDescription", item.getFgiDescription());
			json.put("FGIPriceCode", item.getFgiPrice());
			json.put("FGIQtyAllocated", item.getFgiQtyAllocated());
			json.put("FGIQtyInProduction", item.getFgiQtyInProduction());
			json.put("FGIQtyOnHand", item.getFgiQtyOnHand());
			json.put("servingTankVolume", item.getServingVesselVolume());
			json.put("version", item.getVersion());

			BeerViewPresenter beerPresenter = new BeerViewPresenter();
			json.put("FGIBeerID", beerPresenter.createJSON(item.getBeer()));
			
			PackageTypePresenter ptPresenter = new PackageTypePresenter();
			json.put("FGIPackageID", ptPresenter.createJSON(item.getPackageType()));
			
			//Create Spcl Price Collection
			if(item.getSpecialPrices()!=null){
				SpclPricePresenter presenter = new SpclPricePresenter();
				if(item.getSpecialPrices().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getSpecialPrices().size(); i++){
						jsonArray.put(presenter.createJSON(item.getSpecialPrices().get(i)));
					}
					json.put("spclPriceCollection", jsonArray);
				}
			}
			//Create Inv Usage Collection
			if(item.getInventoryUsages()!=null){
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(item.getInventoryUsages().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getInventoryUsages().size(); i++){
						jsonArray.put(presenter.createJSON(item.getInventoryUsages().get(i)));
					}
					json.put("invUsageCollection", jsonArray);
				}
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public FGI getEmptyEntity(){
		FGI entity = new FGI();
		return entity;
	}
}
